package com.study.uploadexcel.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.study.uploadexcel.model.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.DateUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author weixiaochun
 */
@Slf4j
public class ExcelUtil {


    public void upLoadExcel(HttpServletResponse response) throws URISyntaxException, IOException {

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("水水水水水", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        OutputStream outputStream = response.getOutputStream();

        String reportDate = LocalDate.now().toString();
        // 模板文件
        String templateFile = ExcelUtil.class.getClassLoader().getResource("doc/模板.xlsx").toURI().getPath();
        // 结果文件，省去了根据模板文件生成的步骤
        String resultFile = "C:\\Users\\18234\\Downloads/ss.xlsx";
        // 根据模板文件生成目标文件
        ExcelWriter excelWriter = EasyExcel
                .write(outputStream)
                .withTemplate(templateFile)
                .build();

        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        // 每次都会重新生成新的一行，而不是使用下面的空行
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        // 第一种占位符替换
        Map<String, Object> map = new HashMap<>();
        map.put("date", reportDate);
        excelWriter.fill(map, writeSheet);
        // 第二种占位符替换，这里定义了 hisData
        excelWriter.fill(new FillWrapper("testData", hisData()), fillConfig, writeSheet);
        excelWriter.finish();
    }

    private static List<User> hisData(){
        List<User> resList = new ArrayList<>();
        String today = LocalDate.now().toString();
        String yesterday = LocalDate.now().plusDays(-1).toString();
        User user = new User();
        user.setSeq(1);
        user.setName("张三");
        user.setSex("男");
        user.setTel("17896542369");
        user.setAddress("山西");
        user.setT1("t1");
        user.setT2("t2");
        user.setT3("t3");
        user.setT4("t4");
        resList.add(user);
        user = new User();
        user.setSeq(2);
        user.setName("李四");
        user.setSex("女");
        user.setTel("17896547896");
        user.setAddress("北京");
        user.setT1("t11");
        user.setT2("t22");
        user.setT3("t33");
        user.setT4("t44");
        resList.add(user);
        return resList;
    }
}
